// Copyright (C) 2005 Taylor L. Riché <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// 	$Id: Container.hh 345 2005-11-07 03:20:11Z richetl $	
//

#ifndef CONTAINER_HH
#define CONTAINER_HH

#ifdef LEE_BUILD
#include "Subscription.hh"
#include "Synchronized.hh"
#else
#include <lee/Subscription.hh>
#include <lee/Synchronized.hh>
#endif

namespace lee
{

  class Function;
  class Composable;
  class Module;

  class Container : public Synchronized, public Subscription
  {
    friend class Scheduler;
    void dealWithModule(Module * m, Container * c);
  protected:
    void registerComposableSubs(Composable * comp);
    std::map<std::string, Composable*> composableMap;
    std::map<std::string, std::map<int, Composable*> *> appGraph;
    bool mapSet;
  public:
    Container() : mapSet(false) {}
    virtual ~Container() {}
    void checkSet() {
      Synchronized::Scope mutex(this);
      while(!mapSet) {
 	wait();
      }
      return;
    }
    bool getSet() {return mapSet;}
    void setMapSet(bool ms) {mapSet = ms;}
    void setCMap(std::map<pthread_t, CourierUnit*> const & cmap);
    Composable * getNextComposable(std::string composableName, int exitPoint);
    template<typename T> void createComposable(Container * cont, std::string composableName,
					       std::string cT)
    {
      //Synchronized::Scope mutex(this);
      //       T * function = new T(functionName);
      //       cont->functionMap[functionName] = function;
      //       registerFunctionSubs(dynamic_cast<Function *>(function));

      T * composable = new T(composableName, cT);
      composable->setCont(this);
      Module * mod = dynamic_cast<Module *>(composable);
      if(mod != NULL) {
	//mod->formInnerComponents(cont);
	dealWithModule(mod, cont);
      }
      //else {
      cont->composableMap[composableName] = composable;
      registerComposableSubs(composable);
      //}
      
      //notifyAll();
      return;
    }
    virtual void makeConnection(std::string leftComposable, int leftPort, std::string rightComposable) = 0;
  };

}

#endif // CONTAINER_HH
